require(
    [
        '../../js/lib/gojs/go-debug'
    ],
    function (go) {

        var $$ = go.GraphObject.make;

        var diagram =
            $$(go.Diagram, "main",
                { initialContentAlignment: go.Spot.Center });



        // These new layers come in front of the standard regular layers,
        // but behind the "Foreground" layer:
        var forelayer = diagram.findLayer("Foreground");
        diagram.addLayerBefore($$(go.Layer, { name: "blue" }), forelayer);
        diagram.addLayerBefore($$(go.Layer, { name: "green" }), forelayer);
        diagram.addLayerBefore($$(go.Layer, { name: "orange" }), forelayer);

        diagram.nodeTemplate =
            $$(go.Part,  // no links or grouping, so can use the simpler Part class
                new go.Binding("layerName", "color"),
                new go.Binding("location", "loc"),
                $$(go.Shape,
                    { width: 100, height: 100 },
                    new go.Binding("fill", "color")),
                $$(go.TextBlock,
                    new go.Binding("text", "", function(x) { return x.layer.name; })
                        .ofObject()),
                {
                    selectionChanged: function(p) {
                        if (p.isSelected) p.layerName = "Foreground";
                        else p.layerName = p.data.color;
                    }
                }
            );

        var array = [];
        for (var i = 0; i < 12; i++) {
            var data = { loc: new go.Point(Math.random()*500, Math.random()*150) };
            switch (Math.floor(Math.random()*3)) {
                case 0: data.color = "blue"; break;
                case 1: data.color = "green"; break;
                case 2: data.color = "orange"; break;
                default: data.color = "Foreground"; break;
            }
            array.push(data);
        }
        diagram.model.nodeDataArray = array;
        diagram.undoManager.isEnabled = true;

        // define this function so that the checkbox event handlers can call it
        toggleVisible = function(layername, e) {
            diagram.startTransaction('toggle ' + layername);
            var layer = diagram.findLayer(layername);
            if (layer !== null) layer.visible = e.currentTarget.checked;
            diagram.commitTransaction('toggle ' + layername);
        };

    }
)